package registry

import (
	"errors"
	"fmt"
	"gitee.com/vrv_media/go-micro-framework/registry"
	"gitee.com/vrv_media/go-micro-framework/registry/options"
	"gitee.com/vrv_media/go-micro-framework/registry/registry/consul"
	"gitee.com/vrv_media/go-micro-framework/registry/registry/etcd"
	"gitee.com/vrv_media/go-micro-framework/registry/registry/eureka"
	"gitee.com/vrv_media/go-micro-framework/registry/registry/kubernetes"
	"gitee.com/vrv_media/go-micro-framework/registry/registry/nacos"
	"gitee.com/vrv_media/go-micro-framework/registry/registry/zookeeper"
	"strings"
)

func GetRegistryWithRegistryOpts(registryOps *options.RegistryOptions) (registry.Registrar, error) {
	if registryOps == nil {
		return nil, errors.New("registry is nil")
	}
	err := registryOps.Validate()
	if err != nil {
		return nil, err
	}
	DiscoveryType := strings.ToLower(string(registryOps.Type))
	switch DiscoveryType {
	case string(options.RegistryDiscoveryTypeConsul):
		return consul.NewConsulRegistry(registryOps), nil
	case string(options.RegistryDiscoveryTypeEtcd):
		return etcd.NewEtcdRegistry(registryOps), nil
	case string(options.RegistryDiscoveryTypeEureka):
		return eureka.NewEurekaRegistry(registryOps), nil
	case string(options.RegistryDiscoveryTypeKubernetes):
		return kubernetes.NewKubernetesRegistry(registryOps)
	case string(options.RegistryDiscoveryTypeNacos):
		return nacos.NewDefaultNacosRegistry(registryOps), nil
	case string(options.RegistryDiscoveryTypeZookeeper):
		return zookeeper.NewDefaultZookeeperRegistry(registryOps), nil
	default:
		errMsg := fmt.Sprintf("not supported registry type:%s", DiscoveryType)
		return nil, errors.New(errMsg)
	}
}
